var express = require('express');
var router = express.Router();
let db = require('../utils/db');//打开数据库连接\
let connection = db.connection();//打开数据库连接
let appConfig = require('../utils/app-config');//操作数据库
let basic =require("../utils/basic.js");
var loggerInfo = require('../utils/winston-date').logger;// 自定义打印 包含日志文件分割功能
var loggerError = require('../utils/winston-date').loggerError;// 自定义打印 包含日志文件分割功能
//用户登录
router.post('/login', function (req, res, next) {
  var body = req.body;
  if (!body.userName || !body.password) {
    res.json(Object.assign(appConfig.errorJson(), { msg: "用户名或密码错误" }));
  }
  connection.query("SELECT count(*) FROM `userBasic` where userName='" + body.userName + "' and `password`='" + body.password + "'", (e, d, f) => {
    if (e) { res.json(appConfig.errorJson(e)); }
    var token = basic.newGuid()+basic.newGuid()+basic.newGuid();
    req.session.token = token;
    res.json(Object.assign(appConfig.successJson(), { data: { token: token } }));
  });
  //console.log(req.session,req.session.user);
});

/***************************************京东**************************************************/

/**
 * 注册用户
 * userName 用户名 (必须)
 * passWord 密码 (必须)
 * mobile   手机号 (必须)
 */
router.post('/regUserForJD', function (req, res, next) {
  var userId = basic.newGuid();
  var userName = req.body.userName;
  var passWord = req.body.passWord;
  var mobile = req.body.mobile;
  if (!userName) {
      res.json(Object.assign(appConfig.errorJson(), { msg: "用户名不能为空" }));
      return false;
  }
  if (!passWord) {
      res.json(Object.assign(appConfig.errorJson(), { msg: "密码不能为空" }));
      return false;
  }
  if (!mobile) {
      res.json(Object.assign(appConfig.errorJson(), { msg: "手机号不能为空" }));
      return false;
  }
  var insertSQL = 'INSERT INTO  VALUES ("' + userId + '", "' + userName + '",NULL ,"' + mobile + '" , "' + passWord + '");'
  connection.query(insertSQL, function (e, d, f) {
      if (e) {
          loggerError.error("注册" + mobile + "   ", e);
          res.json(Object.assign(appConfig.errorJson(), { msg: "系统出错" }));
          return false;
      }
      res.json(Object.assign(appConfig.successJson(), { msg: "注册成功" }));
  });
})

/**
 * 登录(通过账号密码登录)
 * userName 用户名 (必须)
 * passWord 密码 (必须)
 */
router.post('/loginForJD', function (req, res, next) {
  loggerInfo.info(userName+"用户开始登录")
  var userName = req.body.userName;
  var passWord = req.body.passWord;
  if (!userName) {
      res.json(Object.assign(appConfig.errorJson(), { msg: "用户名不能为空" }));
      return false;
  }
  if (!passWord) {
      res.json(Object.assign(appConfig.errorJson(), { msg: "密码不能为空" }));
      return false;
  }
  var querySQL = 'SELECT * FROM `jduser` where userName="' + userName + '" and `passWord`="' + passWord + '";';
  connection.query(querySQL, function (e, d, f) {
      if (e) {
          loggerError.error(querySQL," 登录失败" + userName + "   ", e);
          res.json(Object.assign(appConfig.errorJson(), { msg: "系统出错" }));
          return false;
      }
      if(d&&d.length>0){
          var token = basic.newGuid() + basic.newGuid() + basic.newGuid();
          req.session.token = token;
          res.json(Object.assign(appConfig.successJson(), { data: {userName:userName,mobile:d[0].mobile,userId:d[0].userId, token: token } }));
          return;
      }else{
          res.json(Object.assign(appConfig.errorJson(), { msg: "账号密码错误或账号不存在" }));
          return false;
      }
  });
})

/**
 * 忘记密码( 后期通过手机号 )
 * userName 用户名 (必须)
 * passWord 密码 (必须)
 */
router.post('/forGetPassWordForJD', function (req, res, next) {
 
})

module.exports = router;
